package com.zhang.family_doctor.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import com.zhang.family_doctor.entity.Doctor;
import com.zhang.family_doctor.entity.Resident;
import com.zhang.family_doctor.entity.Treaty;

import java.util.Date;

public class TokenUtils {
    //token到期时间6小时
    private static final long EXPIRE_TIME = 6 * 60 * 60 * 1000;
    //医生密钥盐
    private static final String TOKEN_SECRET_DOCTOR = "ljdyaishijin**3nkjnj??";
    //居民密码盐
    private static final String TOKEN_SECRET_RESIDENT = "zxcvbnmashudh**3hvfvfuu";
    //后台管理密码盐
    private static final String TOKEN_SECRET_BACKSTAGE = "qwtryuerugiogbi**3guhttijbj";

    /**
     * 生成token 医生端
     *
     * @param doctor
     * @return
     */
    public static String signDoctor(Doctor doctor) {

        String tokenDoctor = null;
        try {
            Date expireAt = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            tokenDoctor = JWT.create()
                    //发行人
                    .withIssuer("auth0")
                    //存放医生名称
                    .withClaim("Name", doctor.getName())
                    //过期时间
                    .withExpiresAt(expireAt)
                    .sign(Algorithm.HMAC256(TOKEN_SECRET_DOCTOR));
        } catch (IllegalArgumentException | JWTCreationException je) {

        }
        return tokenDoctor;
    }

    /**
     * 生成token 居民端
     *
     * @param resident
     * @return
     */
    public static String signResident(Resident resident) {

        String tokenResident = null;
        try {
            Date expireAt = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            tokenResident = JWT.create()
                    //发行人
                    .withIssuer("auth0")
                    //存放登录的手机号
                    .withClaim("phone",resident.getPhone())
                    //存放登录的居民id
                    .withClaim("id",String.valueOf(resident.getId()))
                    //过期时间
                    .withExpiresAt(expireAt)
                    .sign(Algorithm.HMAC256(TOKEN_SECRET_RESIDENT));
        } catch (IllegalArgumentException | JWTCreationException je) {

        }
        return tokenResident;
    }

    /**
     * 生成token 后台
     *
     * @param doctor
     * @return
     */
    public static String signBackstage(Doctor doctor) {

        String tokenBackstage = null;
        try {
            Date expireAt = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            tokenBackstage = JWT.create()
                    //发行人
                    .withIssuer("auth0")
                    //存放用户名称
                    .withClaim("Name", doctor.getName())
                    .withClaim("phone",doctor.getPhone())
                    .withClaim("doctorId", String.valueOf(doctor.getId()))
                    //过期时间
                    .withExpiresAt(expireAt)
                    .sign(Algorithm.HMAC256(TOKEN_SECRET_BACKSTAGE));
        } catch (IllegalArgumentException | JWTCreationException je) {

        }
        return tokenBackstage;
    }


    /**
     * token验证
     *
     * @param tokenDoctor
     * @return
     */
    public static Boolean verifyDoctor(String tokenDoctor) {

        try {
            //创建token验证器
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET_DOCTOR)).withIssuer("auth0").build();
            DecodedJWT decodedJWT = jwtVerifier.verify(tokenDoctor);
            System.out.println("认证通过：");
            System.out.println("name: " + decodedJWT.getClaim("Name").asString());
            System.out.println("过期时间：      " + decodedJWT.getExpiresAt());
        } catch (IllegalArgumentException | JWTVerificationException e) {
            //抛出错误即为验证不通过
            return false;
        }
        return true;
    }

    /**
     * token验证
     *
     * @param tokenResident
     * @return
     */
    public static Boolean verifyResident(String tokenResident) {

        try {
            //创建token验证器
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET_RESIDENT)).withIssuer("auth0").build();
            DecodedJWT decodedJWT = jwtVerifier.verify(tokenResident);
            System.out.println("认证通过：");
            System.out.println("name: " + decodedJWT.getClaim("Name").asString());
            System.out.println("过期时间：      " + decodedJWT.getExpiresAt());
        } catch (IllegalArgumentException | JWTVerificationException e) {
            //抛出错误即为验证不通过
            return false;
        }
        return true;
    }

    /**
     * token验证
     *
     * @param tokenBackstage
     * @return
     */
    public static Boolean verifyBackstage(String tokenBackstage) {

        try {
            //创建token验证器
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET_BACKSTAGE)).withIssuer("auth0").build();
            DecodedJWT decodedJWT = jwtVerifier.verify(tokenBackstage);
            System.out.println("认证通过：");
            System.out.println("name: " + decodedJWT.getClaim("Name").asString());
            System.out.println("过期时间：      " + decodedJWT.getExpiresAt());
        } catch (IllegalArgumentException | JWTVerificationException e) {
            //抛出错误即为验证不通过
            return false;
        }
        return true;
    }

    public static String verifyAndGetPhone(String tokenResident) {

        try {
            //创建token验证器
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET_RESIDENT)).withIssuer("auth0").build();
            DecodedJWT decodedJWT = jwtVerifier.verify(tokenResident);
            return decodedJWT.getClaim("phone").asString();
        } catch (IllegalArgumentException | JWTVerificationException e) {
            //抛出错误即为验证不通过
        }
        return null;
    }

    /**
     * 从token中拿id
     * @param tokenResident
     * @return
     */
    public static String verifyAndGetId(String tokenResident) {

        try {
            //创建token验证器
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET_RESIDENT)).withIssuer("auth0").build();
            DecodedJWT decodedJWT = jwtVerifier.verify(tokenResident);
            return decodedJWT.getClaim("id").asString();
        } catch (IllegalArgumentException | JWTVerificationException e) {
            //抛出错误即为验证不通过
        }
        return null;
    }

    public static String verifyGetPhone(String tokenBackstage) {

        try {
            //创建token验证器
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET_RESIDENT)).withIssuer("auth0").build();
            DecodedJWT decodedJWT = jwtVerifier.verify(tokenBackstage);
            return decodedJWT.getClaim("phone").asString();
        } catch (IllegalArgumentException | JWTVerificationException e) {
            //抛出错误即为验证不通过
        }
        return null;
    }

    public static String verifyAndGetName(String tokenBackstage) {

        try {
            //创建token验证器
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET_BACKSTAGE)).withIssuer("auth0").build();
            DecodedJWT decodedJWT = jwtVerifier.verify(tokenBackstage);
            return decodedJWT.getClaim("Name").asString();
        } catch (IllegalArgumentException | JWTVerificationException e) {
            //抛出错误即为验证不通过
        }
        return null;
    }

    public static String verifyAndGetDoctorName(String tokenDoctor) {

        try {
            //创建token验证器
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET_DOCTOR)).withIssuer("auth0").build();
            DecodedJWT decodedJWT = jwtVerifier.verify(tokenDoctor);
            return decodedJWT.getClaim("Name").asString();
        } catch (IllegalArgumentException | JWTVerificationException e) {
            //抛出错误即为验证不通过
        }
        return null;
    }

    public static String verifyAndGetDoctorId(String tokenBackstage) {

        try {
            //创建token验证器
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET_BACKSTAGE)).withIssuer("auth0").build();
            DecodedJWT decodedJWT = jwtVerifier.verify(tokenBackstage);
            return decodedJWT.getClaim("doctorId").asString();
        } catch (IllegalArgumentException | JWTVerificationException e) {
            //抛出错误即为验证不通过
        }
        return null;
    }

}
